var t, a = function(t) {
    return t && t.__esModule ? t : {
        default: t
    };
}(require("../../../../utils/toot.js")), e = getApp(), o = 0;

Page({
    data: {
        show: !0,
        hide: !1,
        ClickRseply: !0,
        ClickRseply1: !1,
        tempFilePaths: [],
        imgBoo: !0,
        upFlag: !1,
        oppositeList: [],
        imgUrlArr: [],
        imgUrl: [],
        loadingBoo: !0,
        moreBoo: !0,
        scrollTop: 2e3,
        msgIdArr: [],
        seqIdArr: [],
      // 模拟的个人信息数据
      contactList: [
        {
          patientSimpleVo: {
            userIcon: '/assets/images/head.png',
            name: '小米',
            nmGender: 1,
            age: 30
          },
          dpMedOrderVo: {
            msgTxt: '十万个冷笑话',
            msgPicSmall: ['/assets/images/head.png', '/assets/images/head.png'],
            sessionDateBegin: '2018-10-22-15:06'
          }
        }
      ],
      // 提示消息
      oppositeList:[
        {
          from: 1,
          parting: 1,
          ctime: '小花',
          msgType: 'SINGLE_VIDEO',
          msg: '你好',
          playFlag: '',
          duration: '哈哈'
        },
        {
          from: 1,
          parting: 1,
          ctime: '小花',
          msgType: 'SINGLE_TEXT',
          msg: '你好',
          playFlag:'呵呵哒',
          duration:'哈哈'
        },
        {
          from: 1,
          parting: 1,
          ctime: '小花',
          msgType: 'SINGLE_VIDEO',
          msg: '你好',
          playFlag: '呵呵哒',
          duration: '哈哈'
        },
        {
          from: 1,
          parting: 1,
          ctime: '小花',
          msgType: 'SINGLE_IMAGE',
          msg: '你好',
          playFlag: '呵呵哒',
          duration: '哈哈'
        }
      ]
    },
    Clickshow: function(t) {
        this.setData({
            show: !0,
            hide: !1
        });
    },
    Clickshow1: function(t) {
        this.setData({
            show: !1,
            hide: !0
        });
    },
    ClickRseply: function(t) {
        this.setData({
            ClickRseply: !1,
            ClickRseply1: !0
        });
    },
    lookPaint: function() {
        var t = this;
        wx.request({
            url: e.globalData.doctor + "doctor/manage/showPatientAllowArchives/" + t.data.patientId,
            success: function(a) {
                console.log(a), 200 == a.data.code && (1 == a.data.data ? (wx.setStorageSync("patientId", t.data.patientId), 
                wx.navigateTo({
                  url: "/hyb_yl/index/healthRecord/healthRecord"
                })) : 0 == a.data.data && wx.showModal({
                    title: "温馨提示",
                    content: "您未有查看权限，需患者授权~",
                    confirmColor: "#3b4ca9"
                }));
            }
        });
    },
    bigImage: function(t) {
        console.log(t);
        var a = this;
        wx.previewImage({
            current: t.target.dataset.bigimg,
            urls: a.data.imgUrlArr
        });
    },
    previewImage: function(t) {
        var a = this.data.contactList.dpMedOrderVo.msgPicBig, o = t.target.dataset.imgindex;
        e.previewImage(t, a, o);
    },
    onLoad: function(t) {
        var o = this;
        if (wx.createSelectorQuery) {
            var n = wx.createSelectorQuery();
            n.select("#mjltest").boundingClientRect(), n.exec(function(t) {
                console.log(t), o.setData({
                    text_height: t[0].height
                }), wx.getSystemInfo({
                    success: function(t) {
                        console.log(t.windowHeight), o.setData({
                            scroll_height: t.windowHeight - o.data.text_height
                        });
                    }
                });
            });
        } else o.setData({
            serviceFlag: !0
        }), wx.getSystemInfo({
            success: function(t) {
                console.log(t.windowHeight), o.setData({
                    scroll_height: t.windowHeight
                });
            }
        });
        console.log(t), o.setData({
            doctorId: t.doctorId,
            sessionId: t.sessionId,
            patientId: t.userId,
            token: wx.getStorageSync("log"),
            signType: t.signType,
            teamMyUserId: t.doctorId
        }), wx.setStorageSync("flag", 1), wx.setStorageSync("patientId", o.data.patientId), 
        a.default.getFirstMsg(o), wx.request({
            url: e.globalData.im + "chat/iplist",
            data: {
                token: o.data.token,
                to: o.data.doctorId,
                medType: o.data.signType
            },
            success: function(t) {
                console.log(t), 200 == t.data.code && (o.setData({
                    chatUrl: t.data.data.chatUrl
                }), o.socket(o));
            }
        }), wx.createInnerAudioContext && (s.onPlay(function() {
            console.log("开始播放"), o.data.oppositeList[o.data.playIndex] && (o.data.oppositeList[o.data.playIndex].playFlag = !0, 
            o.setData({
                oppositeList: o.data.oppositeList
            }));
        }), s.onError(function(t) {
            console.log(t.errMsg), console.log(t.errCode);
        }), s.onStop(function() {
            console.log("停止播放");
        }), s.onEnded(function() {
            console.log("播放完毕了"), o.data.oppositeList[o.data.playIndex] && (o.data.oppositeList[o.data.playIndex].playFlag = !1, 
            o.setData({
                oppositeList: o.data.oppositeList
            }));
        }));
    },
    toPlay: function(t) {
        if (s) {
            var a = this;
            console.log(t), t.currentTarget.dataset.index == this.data.playIndex || !this.data.playIndex && 0 != this.data.playIndex || a.data.oppositeList[a.data.playIndex] && (a.data.oppositeList[a.data.playIndex].playFlag = !1, 
            a.setData({
                oppositeList: a.data.oppositeList
            })), this.data.playIndex = t.currentTarget.dataset.index, s.src = t.currentTarget.dataset.src, 
            this.data.oppositeList[this.data.playIndex].playFlag ? (s.stop(), a.data.oppositeList[a.data.playIndex].playFlag = !1, 
            a.setData({
                oppositeList: a.data.oppositeList
            })) : s.play();
        } else wx.showModal({
            content: "您当前微信版本过低，暂不支持语音功能，请更新微信版本后重试",
            showCancel: !1,
            confirmColor: "#3b4ca9",
            confirmText: "我知道了"
        });
    },
    socket: function(a) {
        wx.connectSocket({
            url: a.data.chatUrl,
            header: {
                "content-type": "application/json"
            },
            method: "GET",
            success: function(t) {
                console.log(t);
            }
        }), wx.onSocketOpen(function(t) {
            console.log("WebSocket连接已打开！");
            var e = JSON.stringify({
                version: 1,
                operation: 1,
                seqId: new Date().getTime(),
                data: {
                    token: a.data.token,
                    to: a.data.patientId
                }
            });
            wx.sendSocketMessage({
                data: e,
                success: function(t) {
                    console.log(t);
                }
            });
        }), wx.onSocketMessage(function(s) {
            var n = JSON.parse(s.data);
            switch (n.operation) {
              case 2:
                200 == n.data.code && (a.setData({
                    userId: n.data.userId
                }), a.pullMsg(0, 10, a)), a.heartbeat(), t = setInterval(a.heartbeat, 5e4);
                break;

              case 4:
                console.log("心跳ok");
                break;

              case 6:
                console.log(n), a.data.msgIdArr.push(n.data.msgId), a.data.seqIdArr.push(parseInt(n.data.seqId));
                break;

              case 8:
                break;

              case 9:
                var i = a.data.oppositeList, l = a.data.imgUrlArr, d = a.data.imgUrl;
                console.log(n.data), n.data.ctime = e.toDate(n.data.ctime), "SINGLE_IMAGE" == n.data.msgType ? (d = n.data.msg.split("|"), 
                n.data.msg = d[0], n.data.bigImg = d[1], l.push(d[1]), a.setData({
                    imgUrlArr: l
                }), i.push(n.data), a.setData({
                    oppositeList: i,
                    scrollTop: a.data.scrollTop + 1e3
                })) : (i.push(n.data), console.log(i), a.setData({
                    oppositeList: i,
                    scrollTop: a.data.scrollTop + 1e3
                }), console.log(a.data.oppositeList));
                var c = {
                    version: 1,
                    operation: 7,
                    seqId: new Date().getTime(),
                    data: {
                        to: n.data.from,
                        msgId: n.data.msgId
                    }
                }, r = JSON.stringify(c);
                wx.sendSocketMessage({
                    data: r,
                    success: function(t) {}
                });
                break;

              case 10:
                break;

              case 14:
                console.log(n), n.data.sessionid == a.data.sessionId && (console.log("条件成立了"), a.setData({
                    disableFlag: !0
                })), console.log("对方结束了本次咨询：sessionId:" + n.data.sessionId + ", from:" + n.data.from + ",msg:" + n.data.msg);
                break;

              case 16:
                console.log(n);
                for (var g = n.data.msgId, i = a.data.oppositeList, p = 0; p < i.length; p++) if (i[p].msgId == g) {
                    i.splice(p, 1), a.setData({
                        oppositeList: i
                    });
                    break;
                }
                console.log("对方撤回了消息，sessionId:" + n.data.sessionId + ", from:" + n.data.from + ",msgId:" + n.data.msgId);
                break;

              case 12:
                var u = [];
                if (null != n.data.list && (u = n.data.list, console.log(n.data.list), u.forEach(function(t, e) {
                    var s = a.data.oppositeList, n = a.data.imgUrlArr, i = a.data.imgUrl;
                    console.log(o), 0 == e && 0 == o && (t.parting = 1), o++, t.msg = t.message, t.from = t.msgFrom, 
                    "SINGLE_IMAGE" == t.msgType ? (i = t.msg.split("|"), t.msg = i[0], n.push(i[1]), 
                    t.bigImg = i[1], s.push(t)) : s.push(t), a.setData({
                        oppositeList: s,
                        imgUrlArr: n,
                        scrollTop: a.data.scrollTop + 2e3
                    });
                })), null != n.data.list && n.data.list.length > 0) {
                    g = n.data.list[n.data.list.length - 1].msgId;
                    console.log("msgId:" + g), a.pullMsg(g, 10, a);
                } else wx.closeSocket({
                    success: function(t) {
                        console.log(t);
                    }
                }), wx.onSocketClose(function(a) {
                    console.log("WebSocket 已关闭！"), console.log(a), clearInterval(t);
                });
            }
        });
    },
    heartbeat: function() {
        var t = {
            version: 1,
            operation: 3,
            seqId: new Date().getTime(),
            data: {}
        }, a = JSON.stringify(t);
        wx.sendSocketMessage({
            data: a,
            success: function(t) {}
        });
    },
    pullMsg: function(t, a, e) {
        console.log(e.data.sessionId);
        var o = {
            version: 1,
            operation: 11,
            seqId: new Date().getTime(),
            data: {
                userId: e.data.userId,
                offset: t,
                limit: a,
                sessionId: e.data.sessionId,
                medType: e.data.signType
            }
        }, s = JSON.stringify(o);
        wx.sendSocketMessage({
            data: s,
            success: function(t) {}
        });
    },
    onUnload: function() {
        wx.closeSocket({
            success: function(t) {
                console.log(t);
            }
        }), wx.onSocketClose(function(a) {
            console.log("WebSocket 已关闭！"), console.log(a), clearInterval(t);
        });
    },
    scrolltoupper: function() {
        var t = this;
        a.default.scrolltoupper(t);
    },
    onReady: function() {},
    onShow: function() {
        o = 0;
    }
});

var s = wx.createInnerAudioContext ? wx.createInnerAudioContext() : null;